package com.og.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.CascadeType;

import org.apache.log4j.Logger;

import com.og.backingbean.Application;
import com.og.backingbean.Composition;
import com.og.backingbean.Module;
import com.og.backingbean.Person;
import com.og.dao.CRUDEntityDAO;

/**
 * @author Renaud DIANA
 *
 */
@Named
@SessionScoped
public class CompositionBean implements Serializable {

	private static final long serialVersionUID = 1L;

	// Log4j	 
	private static final Logger	log	= Logger.getLogger(CompositionBean.class);

	/* Dependency injection */
	// Model
    @Inject
    private CRUDEntityDAO<Composition> compositionDAO;  
    @Inject
    private CRUDEntityDAO<Application> applicationDAO;
    @Inject
    private CRUDEntityDAO<Module> moduleDAO;
    // BackingBean    
    @Inject
    private Application application;
    @Inject
    private Module module;
    @Inject
    private Composition compositionNew;
    @Inject
    private Composition compositionUpdate;
    @Inject
    private Composition compositionView;
    @Inject
    private ListBean list;
        
    /* -------------------- */
	    
	/**
	 * Default constructor
	 */
	public CompositionBean() {
		// TODO Auto-generated constructor stub
	}

	public String create() {
		
		Date date = new Date();
		compositionNew.setDate(date);
		
		// Set List
		List<Composition> listCompoApp = compositionNew.getApplication().getListComposition();
		listCompoApp.add(compositionNew);
		compositionNew.getApplication().setListComposition(listCompoApp);
		
		List<Composition> listCompoMod = compositionNew.getModule().getListComposition();
		listCompoMod.add(compositionNew);
		compositionNew.getModule().setListComposition(listCompoMod);
		
		// Call model				
		// Application or Module DAO can be called -> OneToMany(cascade = CascadeType.ALL)
		// !!ManyToOne(cascade = CascadeType.ALL) in Composition.java and compositionDAO.create() doesn't work!!
		applicationDAO.updateEntity(compositionNew.getApplication());		
		
		// Generate new list
		list.findAllComposition();
		list.findAllApplication();
		list.findAllModule();
		
		// Clear form
		compositionNew = new Composition();
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create composition Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}	
	
	public String update() {
		
		// Call model				
		compositionDAO.updateEntity(compositionUpdate);
		
		// Toggle view
		compositionView = compositionUpdate;
				
		// Generate new list
		list.findAllComposition();
			
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update composition Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String delete() {
				
		// Set List
		List<Composition> listCompoApp = compositionNew.getApplication().getListComposition();
		listCompoApp.remove(compositionNew);
		compositionNew.getApplication().setListComposition(listCompoApp);
		
		List<Composition> listCompoMod = compositionNew.getModule().getListComposition();
		listCompoMod.remove(compositionNew);
		compositionNew.getModule().setListComposition(listCompoMod);
				
		// Call model				
		compositionDAO.deleteEntity(compositionView);
		applicationDAO.updateEntity(compositionNew.getApplication());
		moduleDAO.updateEntity(compositionNew.getModule());		
		
		// Generate new list
		list.findAllComposition();
		
		// Clear form
		compositionView = new Composition();
				
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete composition Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String findOne() {
		
		// Call model				
		compositionView = compositionDAO.findOneEntity(compositionView, compositionView.getIdComposition());
					
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Find composition Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}

	public String toggleUpdate() {
		
		compositionUpdate = compositionView;
		
		return "";
	}

	// Getters and Setters
	
	public Application getApplication() {
		return application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public Composition getCompositionNew() {
		return compositionNew;
	}

	public void setCompositionNew(Composition compositionNew) {
		this.compositionNew = compositionNew;
	}

	public Composition getCompositionUpdate() {
		return compositionUpdate;
	}

	public void setCompositionUpdate(Composition compositionUpdate) {
		this.compositionUpdate = compositionUpdate;
	}

	public Composition getCompositionView() {
		return compositionView;
	}

	public void setCompositionView(Composition compositionView) {
		this.compositionView = compositionView;
	}
		
}
